<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="login_form">
        <el-form :model="loginForm" :rules="loginFormRules" ref="ruleFormRef">
          <el-form-item prop="username">
          <!--账号-->
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="用户名"></el-input>
          </el-form-item>
        <el-form-item prop="password">
          <!--密码-->
          <el-input v-model="loginForm.password" placeholder="密码" type="password"
                    prefix-icon="iconfont icon-3702mima" show-password></el-input>
        </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login()">登录</el-button>
            <el-button type="info" @click="resetLoginForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      // 登录表单数据的绑定
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单验证的规则
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // console.log(this)
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.ruleFormRef.resetFields()
    },
    login () {
      this.$refs.ruleFormRef.validate(async (valid, obj) => {
        // console.log(valid, obj)
        if (!valid) return
        const { data: result } = await this.$http.post('login', this.loginForm)
        // console.log(result)
        if (result.meta.status !== 200) return this.errTest()
        this.$message.success('登录成功')
        // 1.登陆成功后将token保存到客户端的sessionStorage中
        // console.log(result.data.token)
        window.sessionStorage.setItem('token', result.data.token)
        // 2.通过编程式导航跳转到后台主页
        await this.$router.push('/home')
      })
    },
    errTest () {
      this.$message({
        showClose: true, // 是否显示关闭按钮
        message: '登录失败',
        offset: 10, // 距离顶部的偏移量
        center: true, // 文字居中
        type: 'error'
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  //flex布局 子盒子水平垂直居中
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: relative;
  //position + transform  子盒子水平垂直居中
  //position: absolute;
  //left: 50%;
  //top: 50%;
  //transform: translate(-50%,-50%);
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  width: 100%;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
